package org.zjx.command;

import org.zjx.command.basic.ScreenshotCommand;
import org.zjx.core.IWindow;
import org.zjx.state.StateManager;
import org.zjx.strategy.IStrategy;
import org.zjx.strategy.StrategyFactory;

import java.awt.image.BufferedImage;

public class StrategyCommand extends Command {
    private final StateManager stateManager;

    public StrategyCommand(IWindow targetWindow, StateManager stateManager) {
        super(targetWindow, false, stateManager);
        this.stateManager = stateManager;
    }

    @Override
    protected void doExecute() throws Exception {
        ScreenshotCommand screenshotCmd = new ScreenshotCommand(targetWindow);
        screenshotCmd.execute();
        BufferedImage screenshot = screenshotCmd.getScreenshot();

        IStrategy strategy = StrategyFactory.getInstance()
                .selectAndExecuteStrategy(targetWindow, screenshot, stateManager);

        ICommand nextCommand = strategy.analyze(targetWindow, screenshot, stateManager);
        if (nextCommand != null) {
            targetWindow.submitCommand(nextCommand);
        }
    }
}
